<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Menu_lib {
	
	protected $CI;
	
	public function __construct()
	{
		// Carregando a library session
		$this->CI =& get_instance();
		
		$this->CI->load->model('Vinculo_Perfil_Menu_Model', 'vinculo');
		include_once 'app/enum/EnumTipoPerfil.php';
	}
	
	public function menuTopo(){
		
		try {
			
			$tipoPerfil = decrypt(trim($this->CI->session->userdata('tipoPerfil')));
			
			$html = '   <div class="navbar-header">
							<a class="navbar-minimalize minimalize-styl-2 btn btn-primary " href="#"><i class="fa fa-bars"></i> </a>
							<form role="search" class="navbar-form-custom" action="search_results.html">
				                <div class="input-group col-lg-12">
				                    <input type="text" placeholder="Search..." class="form-control" name="top-search" id="top-search">
								</div>
				            </form>
						</div>
						<ul class="nav navbar-top-links navbar-right">';
			
			//Se o perfil for do tipo Administrador Geral
			if ((int)$tipoPerfil === (int)EnumTipoPerfil::getCodigo(EnumTipoPerfil::ADMINISTRADOR_FULL)){
				$html .='		<li>
									<a href="'.base_url().'phpInfo" target="_blank">
										<i class="fa fa-file-code-o"></i>PHP
									</a>
								</li>';
			}
			
			$html .='		<li>
								<a href="'.base_url().'logout">
									<i class="fa fa-power-off"></i> Log out
								</a>
							</li>
						</ul>';
			
			return $html;
			
		} catch (Exception $e) {
			throw $e;
		}
		
	}
	
	public function menuLateral($classe){
		
		try {
			
			$nome = decrypt(trim($this->CI->session->userdata('nome')));
			$foto = decrypt(trim($this->CI->session->userdata('foto')));
			
			//Recupera perfil da sessão
			$idPerfil = decrypt(trim($this->CI->session->userdata('idPerfil')));
			
			$html = '<ul class="nav" id="side-menu">
						<li class="nav-header">
							<div class="dropdown profile-element"> 
								<span>
									<img id="fotoAdmin" alt="Foto" class="img-circle" src="'.$foto.'" height="48px" width="48px" />
								</span>
								<a data-toggle="dropdown" class="dropdown-toggle" href="#">
									<span class="clear"> 
										<span class="block m-t-xs"> 
											<strong class="font-bold" style="color: #ffffff;">'.$nome.'</strong>
									 	</span> 
									</span> 
								</a>
								<ul class="dropdown-menu animated fadeInRight m-t-xs">
									<li>
										<a href="'.base_url().'admin/profile" onclick="deleteCookie();">
											<i class="fa fa-user"></i>&nbsp;&nbsp;Profile
										</a>
									</li>
									<li>
										<a href="'.base_url().'admin/alterar_senha" onclick="deleteCookie();">
											<i class="fa fa-key"></i>&nbsp;&nbsp;Alterar Senha
										</a>
									</li>
									<li class="divider"></li>
									<li>
										<a href="'.base_url().'logout">
											<i class="fa fa-power-off"></i>&nbsp;&nbsp;Logout
										</a>
									</li>
								</ul>
							</div>
						</li>';
			
			$html .= $this->montaMenuPai($idPerfil, $classe);
			
			$html .= '</ul>';
						
			return $html;
			
		} catch (Exception $e) {
			throw $e;
		}
	}
	
	public function caminhoPao($classe, $menu, $metodo){
	
		try {
			
			//Recupera perfil da sessão
			$idPerfil = decrypt(trim($this->CI->session->userdata('idPerfil')));
				
			$html = '<div class="row wrapper border-bottom white-bg page-heading">
						<div class="col-sm-12" style="margin-top: 18px;">
							<ol class="breadcrumb">
								<li>
									<a href="'.base_url().'admin">Home</a>
								</li>';
				
			if (!empty($classe)){
				
				$html .= $this->montaCaminhoPao($idPerfil, $classe, $metodo);
	
			}else{
				if (!empty($menu)){
					$html .= '<li class="active">
									<strong>'.$menu.'</strong>
							  </li>';
				}
			}
				
			$html .= '		</ol>
						</div>
					</div>';
			
			return $html;
	
		} catch (Exception $e) {
			throw $e;
		}
	}
	
	private function montaCaminhoPao($idPerfil, $classe, $metodo){
		
		try {
			
			$html = "";
			
			$arrayMenus = array();
			
			$menuSelecionado = $this->CI->vinculo->obterMenuPaiPorClassePerfil($classe, $idPerfil);
			
			if (!empty($menuSelecionado)){
			
				if (!empty($menuSelecionado->idMenuPai) && $menuSelecionado->idMenuPai !== null){
					
					$idMenuPai = $menuSelecionado->idMenuPai;
					
					do{
						$menu =  $this->CI->vinculo->obterVinculoPorPerfilIdMenu($idPerfil, $idMenuPai);
							
						//Colocando em um array os menus
						array_push($arrayMenus, $menu->descricao);
						
						$idMenuPai = (empty($menu->idMenuPai)) ? null : $menu->idMenuPai;
						
					}while(!empty($idMenuPai) && $idMenuPai !== null);
					
					//Invertando ordem array
					$arrayMenus = array_reverse($arrayMenus);
					
					foreach ($arrayMenus as $row){
						$html .= '<li>
									  '.$row.'
								  </li>';
					}
					
					if (empty($metodo)){
						$html .= '  <li class="active">
										<strong>'.$menuSelecionado->descricao.'</strong>
									</li>';
					}else{
						$html .= '  <li>
										<a href="'.base_url().'admin/'.$menuSelecionado->classe.'">'.$menuSelecionado->descricao.'</a>
									</li>
									<li class="active">
										<strong>'.$metodo.'</strong>
									</li>';
						
					}
					
				}else{
					if (empty($metodo)){
						$html .= '  <li class="active">
										<strong>'.$menuSelecionado->descricao.'</strong>
									</li>';
					}else{
						$html .= '  <li>
										<a href="'.base_url().'admin/'.$menuSelecionado->classe.'">'.$menuSelecionado->descricao.'</a>
									</li>
									<li class="active">
										<strong>'.$metodo.'</strong>
									</li>';
						
					}
				}
			}
			
			return $html;
			
		} catch (Exception $e) {
			throw $e;
		}
	}
		
	private function montaMenuPai($idPerfil, $classe){
	
		try {
			
			$tipoPerfil = decrypt(trim($this->CI->session->userdata('tipoPerfil')));

			//Busca menus pais
			$menuPai = $this->CI->vinculo->listarMenuPaiPorPerfil($idPerfil);
			
			$html = "";
			
			foreach ($menuPai as $row){
				
				//Busca Menu Filhos
				$menuFilho = $this->CI->vinculo->listarMenuFilhoPorIdMenuPai($idPerfil, $row->idMenu);
				
				//Se não tiver menus filhos
				if (empty($menuFilho)){
					
					$active = "";
					
					if (!empty($classe)){
						$active = (trim($classe) === trim($row->classe)) ? "class='active'" : "";
					}
					
					if (($row->menuRestrito === "S" && (int)$tipoPerfil === (int)EnumTipoPerfil::getCodigo(EnumTipoPerfil::ADMINISTRADOR_FULL)) || $row->menuRestrito === "N"){
						$html .= '  <li '.$active.'>
										<a href="'.base_url().'admin/'.trim($row->classe).'" onclick="deleteCookie();" data-toggle="tooltip" data-placement="right" data-original-title="'.trim($row->descricao).'">
											<i class="'.trim($row->imgMenu).'"></i> 
											<span class="nav-label">'.trim($row->descricao).'</span>
										</a>
									</li>';
					}
					
						
				}else{
					
					$active = "";
					
					if (!empty($classe)){
						$active = $this->validaMenuAtivo($idPerfil, $classe, trim($row->descricao));
					}
					
					$html .= '  <li '.$active.'>
									<a href="#">
										<i class="'.trim($row->imgMenu).'"></i> 
										<span class="nav-label">'.trim($row->descricao).'</span> 
										<span class="fa arrow"></span>
									</a>';
					
					$html .= $this->montaMenuFilho($idPerfil, $menuFilho, 2, $classe);
									
					$html .= '</li>';
				}
			}
			
			return $html;
				
		} catch (Exception $e) {
			throw $e;
		}
		
	}
	
	private function montaMenuFilho($idPerfil, $lista, $level, $classe){
	
		try {
			
			$tipoPerfil = decrypt(trim($this->CI->session->userdata('tipoPerfil')));
			
			$posicao = null;
			
			if ((int)$level === 2){
				$posicao = "second";
			}else if ((int)$level === 3){ 
				$posicao = "third";
			}else{
				return false;
			}
			
			$html = '<ul class="nav nav-'.$posicao.'-level">';
			
			foreach ($lista as $row){
			
				$menuFilho = $this->CI->vinculo->listarMenuFilhoPorIdMenuPai($idPerfil, $row->idMenu);
			
				//Se não tiver filho
				if (empty($menuFilho)){
					
					if (($row->menuRestrito === "S" && (int)$tipoPerfil === (int)EnumTipoPerfil::getCodigo(EnumTipoPerfil::ADMINISTRADOR_FULL)) || $row->menuRestrito === "N"){
						$html .= ' <li>
										<a href="'.base_url().'admin/'.trim($row->classe).'" onclick="deleteCookie();" data-toggle="tooltip" data-placement="right" data-original-title="'.trim($row->descricao).'">
											<i class="'.trim($row->imgMenu).'"></i> 
											<span class="nav-label">'.trim($row->descricao).'</span> 
										</a>
								  </li>';
					}
					
				}else{
					
					$level++;
					
					$active = "";
					
					if (!empty($classe)){
						$active = $this->validaMenuAtivo($idPerfil, $classe, trim($row->descricao));
					}
					
					$html .= ' <li '.$active.'>
		                            <a href="#">
										<i class="'.trim($row->imgMenu).'"></i> 
										<span class="nav-label">'.trim($row->descricao).'</span> 
										<span class="fa arrow"></span>
									</a>';
					
					$html .= $this->montaMenuFilho($idPerfil, $menuFilho, $level, $classe);

		            $html .= ' </li>';
						
				}
			
			}
				
			$html .= '</ul>';
			
			return $html;
	
		} catch (Exception $e) {
			throw $e;
		}
	
	}
	
	private function validaMenuAtivo($idPerfil, $classe, $descMenuPai){
		try {
				
			$active = "";
				
			$menuSelecionado = $this->CI->vinculo->obterMenuPaiPorClassePerfil($classe, $idPerfil);
				
			if (!empty($menuSelecionado)){
	
				if ($descMenuPai === $menuSelecionado){
					return "class='active'";
				}
					
				if (!empty($menuSelecionado->idMenuPai) && $menuSelecionado->idMenuPai !== null){
	
					$idMenuPai = $menuSelecionado->idMenuPai;
						
					do{
						$menu =  $this->CI->vinculo->obterVinculoPorPerfilIdMenu($idPerfil, $idMenuPai);
							
						if ($descMenuPai === $menu->descricao){
							return "class='active'";
						}
							
						$idMenuPai = (empty($menu->idMenuPai)) ? null : $menu->idMenuPai;
							
					}while(!empty($idMenuPai) && $idMenuPai !== null);
	
				}
			}
				
			return $active;
				
		} catch (Exception $e) {
			throw $e;
		}
	}
	
}